package jmine.tec.aom.services.include;

import jmine.tec.aom.domain.PropertyGroup;
import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Includes a new {@link PropertyGroup}
 * 
 * @author jrenaut
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class PropertyGroupService implements ServiceFiller<PropertyGroup> {

    public static final String MNEMONIC = "Mnemonic";

    public static final String IDENTIFICATION = "Identification";

    public static final String DATABASE_ID = "Database ID";

    private PackEntityPersistenceHelper helper;

    private Long databaseID;

    private String mnemonic;

    /**
     * @return the {@link PropertyGroup}
     * @throws PackException if the {@link PropertyGroup} cannot be retrieved or created
     */
    @Execution
    @Output(propertyName = IDENTIFICATION)
    public PropertyGroup execute() throws PackException {
        PropertyGroup grupo = this.helper.findOrCreate(PropertyGroup.class, this.databaseID, this.mnemonic);
        grupo.setMnemonic(this.mnemonic);
        this.helper.save(grupo, this.databaseID);
        return grupo;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, PropertyGroup entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, MNEMONIC, entity.getMnemonic());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, IDENTIFICATION, referenceMap.addReference(entity, "group"));
    }

    /**
     * @param mnemonic the mnemonic
     */
    @Input(fieldName = MNEMONIC)
    public void setMnemonic(String mnemonic) {
        this.mnemonic = mnemonic;
    }

    /**
     * @param databaseID the database ID
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseID(Long databaseID) {
        this.databaseID = databaseID;
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }
}
